package com.group3.merchant.servlet;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.group3.entity.Dish;
import com.group3.merchant.service.IDishService;
import com.group3.merchant.service.impl.DishServiceImpl;
import com.group3.merchant.utils.SenderUtil;

/**
 * Servlet implementation class UpdateDishServlet
 */
public class UpdateDishServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IDishService service = new DishServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateDishServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Dish dish = updateDish(request, response);
		System.out.println("update dish: " + dish);
		Map<String, Object> map = new HashMap<String, Object>();
		if(dish != null) {
//			map.put("dish", dish);
//			map.put("status", "200");
//			SenderUtil.send(map, response);
			response.sendRedirect("mainpage.html");
		} else {
			map.put("status", "401");
		}
		// 转发重定向
//		response.sendRedirect("ListDishServlet");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Dish dish = updateDish(request, response);
		System.out.println("update dish: " + dish);
		Map<String, Object> map = new HashMap<String, Object>();
		if(dish != null) {
//			map.put("dish", dish);
//			map.put("status", "200");
//			SenderUtil.send(map, response);
			response.sendRedirect("mainpage.html");
		} else {
			map.put("status", "401");
		}
		// 转发重定向
//		response.sendRedirect("ListDishServlet");
	}

	private Dish updateDish(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		boolean isMultipart = FileUpload.isMultipartContent(request);
		if (isMultipart) {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			String filePath = "";

			Dish dish = new Dish();
			boolean isNull=false;
			try {
				List<FileItem> list = upload.parseRequest(request);
				Iterator<FileItem> i = list.iterator();
				while (i.hasNext()) {
					FileItem item = i.next();
					if (item.isFormField()) {
						switch (item.getFieldName()) {
						case "dName":
							dish.setdName(item.getString("utf-8"));
							break;
						case "dPrice":
							dish.setdPrice(Double.valueOf(item
									.getString("utf-8")));

							break;
						case "pId":
							dish.setpId(item.getString("utf-8"));
							request.getSession().setAttribute("dType", item.getString("utf-8"));
							break;
						case "imagepath":
//							System.out.println("field");
							if(isNull){
//								System.out.println("if null setURL");
								dish.setdImageURL(item.getString("utf-8"));}
							break;
						case "dId":
							dish.setdId(item.getString("utf-8"));
							break;
						case "mid":
							dish.setmId(item.getString("utf-8"));
							break;
						default:
							break;
						}
					} else {
						switch (item.getFieldName()) {
						case "image":
							if (!"".equals(item.getName())) {
//								System.out.println("not emply");
//								System.out.println("update");
								File imageFile = new File("d://AMC/Dish/"
										+ UUID.randomUUID() + item.getName());
								filePath = imageFile.getAbsolutePath();
								dish.setdImageURL(filePath);
								item.write(imageFile);
							}else {
//								System.out.println("emply");
								isNull=true;
							}
							break;
						default:
							break;
						}
					}
				}
				// 调service
//				request.setAttribute("type", dish.getpId());
//				System.out.println(dish.getpId());
				System.out.println("update servlet dish: " + dish);
				return service.motifyDish(dish);
//				System.out.println(dish);
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return null;
	}
}
